package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.ConfigDao;
import com.stlm2.dems.entity.Config;
import com.stlm2.dems.service.ConfigService;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author：fuchunyu
 * Date：2017/7/21
 * Description：
 */
@Controller
@RequestMapping("configC")
public class ConfigController {
    @Resource
    private ConfigService configService;
    @Resource
    private UserService userService;
    @Resource
    private ConfigDao configDao;

    //跳转到list页
    @RequestMapping("list")
    public ModelAndView list(ModelAndView modelAndView){
        modelAndView.setViewName("/config/list");
        return modelAndView;
    }

    /**
     * Author：fuchunyu
     * Date：2017/7/21
     * Description：config分页查询
     */
    @ResponseBody
    @RequestMapping(value = "/search.json",produces="application/json;charset=UTF-8")
    public String search(ModelMap map,String pageNo,String configName) {

        Config config = new Config();

        int page = 1;
        int pageSize = 10;
        if (NumberUtils.isDigits(pageNo) && !"0".equals(pageNo.trim())) {
            page = NumberUtils.toInt(pageNo);
        }

        if (StringUtils.isNotEmpty(configName))
            config.setConfigName(configName);
        PageView pageview = configService.getPage(page, pageSize, config);
        if (pageview != null) {
            map.put("data",configService.convert(pageview.getRecords()));
            map.put("recordsTotal", pageview.getRowCount());
            map.put("totalPage", pageview.getPageCount());
            map.put("nowPage",pageview.getPageNow());
        }
        return CodeContent.returnSuccess(map);
    }

    /**
     * Author：fuchunyu
     * Date：2017/7/21
     * Description：编辑config配置
     */

    @ResponseBody
    @RequestMapping(value = "edit.json",produces="application/json;charset=UTF-8")
    public String update(String token,String configName, String configValue, String id,String memo){

        User user=userService.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();

        if(!NumberUtils.isDigits(id)){
            CodeContent.returnFailure("非法的ID");
        }
        Config config=configService.findEntityById(Integer.parseInt(id));
        if(StringUtils.isNotEmpty(configName))
            config.setConfigName(configName);
        if(StringUtils.isNotEmpty(configValue))
            config.setConfigValue(configValue);
        if(StringUtils.isNotEmpty(memo))
            config.setMemo(memo);

        config.setUpdateTime(new Date());
        configService.updateEntity(config);

        List<String> list = new ArrayList<String>();
        list.add("configName");
        configDao.addIntoRedis(config, list);

        return CodeContent.returnSuccess(null);
    }

    /**
     * Author：fuchunyu
     * Date：2017/7/21
     * Description：添加config配置
     */

    @ResponseBody
    @RequestMapping(value = "add.json",produces="application/json;charset=UTF-8")
    public String add(String token,String configName, String configValue,String memo){

        User user=userService.getByToken(token);
        if(user==null)
            return CodeContent.returnLogin();

        Config config=new Config();
        if(StringUtils.isNotEmpty(configName)&&StringUtils.isNotEmpty(configValue)){
            config.setConfigName(configName);
            config.setConfigValue(configValue);
        }else{
            return CodeContent.returnFailure("请检查参数名或参数值！");
        }
        if(StringUtils.isNotEmpty(memo))
            config.setMemo(memo);

        config.setCreateTime(new Date());
        config.setCreateUserId(user.getId());
        configService.addEntity(config);

        List<String> list = new ArrayList<String>();
        list.add("configName");
        configDao.addIntoRedis(config, list);

        return CodeContent.returnSuccess(null);
    }


}
